package com.example.springboot.controller;

import com.example.springboot.common.tools.Layui;
import com.example.springboot.common.vo.PageUtils;
import com.example.springboot.entity.SysUser;
import com.example.springboot.service.SysUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (SysUser)表控制层
 *
 * @author makejava
 * @since 2021-09-06 11:55:47
 */
@RestController
@RequestMapping("URMS/sysUser")
public class SysUserController {
	/**
	 * 服务对象
	 */
	@Resource
	private SysUserService sysUserService;

	/**
	 * 通过用户信息查询单条数据
	 *
	 * @param user 账号
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "loginSubmit", method = RequestMethod.POST)
	public SysUser loginSubmit(SysUser user, HttpSession session) {
		//查询相关数据
		user = this.sysUserService.queryByUser(user);
		session.setAttribute("loginUser", user);

		return user;
	}

	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 */
	@RequestMapping(value = "selectOne", method = RequestMethod.POST)
	public SysUser selectOne(Long id) {
		return this.sysUserService.queryById(id);
	}


	/**
	 * 向数据库添加一条信息
	 *
	 * @param sysUser 用户信息
	 * @return 单条数据
	 */
	@RequestMapping(value = "addUserSubmit", method = RequestMethod.POST)
	public boolean addUserSubmit(SysUser sysUser, @RequestParam(value = "roleidList") List<Long> list) {
		return this.sysUserService.insert(sysUser) != null;
	}

	/**
	 * 向数据库查询一页信息
	 *
	 * @return 数据列表
	 */
	@RequestMapping(value = "selectUserList", method = RequestMethod.GET)
	public Layui selectUserList(@RequestParam("page") int page, @RequestParam("limit") int limit) {
		List<SysUser> userList = this.sysUserService.queryAllByLimit((page - 1) * limit, page * limit);
		return Layui.data(this.sysUserService.queryUserCount(), userList);
	}

//	/**
//	 * 向数据库查询一页信息
//	 *
//	 * @param pageQuery 分页信息
//	 * @return 数据列表
//	 */
//	@RequestMapping(value = "selectPageList", method = RequestMethod.GET)
//	public JSONObject selectPageList(PageQuery pageQuery) {
//		return DataTableToJson(this.sysUserService.queryAllByLimit(
//				(pageQuery.getPageCode() - 1) * pageQuery.getPageNumber(),
//				pageQuery.getPageCode() * pageQuery.getPageNumber()), pageQuery.getPageCode());
//	}

	/**
	 * 向数据库查询一页信息
	 *
	 * @param pageUtils 分页信息
	 * @return 数据列表
	 */
	@RequestMapping(value = "selectPageList", method = RequestMethod.GET)
	public Layui selectPageList(@RequestParam PageUtils pageUtils) {
		List<SysUser> userList = this.sysUserService.queryAllByLimit((pageUtils.getPage() - 1) * pageUtils.getLimit(), pageUtils.getPage() * pageUtils.getLimit());
		int count = sysUserService.queryUserCount();
		PageUtils pageUtil = new PageUtils(userList, count, pageUtils.getLimit(), pageUtils.getPage());
		return Layui.data(pageUtil.getCount(), pageUtil.getList());
	}

	/**
	 * 根据id数据库一条信息
	 *
	 * @param id 主键信息
	 * @return 数据列表
	 */
	@RequestMapping(value = "deleteUser", method = RequestMethod.POST)
	public boolean deleteUser(Long id) {
		return sysUserService.deleteById(id);
	}

	/**
	 * 更新数据库一条信息
	 *
	 * @param sysUser 账号信息
	 * @return 数据列表
	 */
	@RequestMapping(value = "editSubmit", method = RequestMethod.POST)
	public boolean editSubmit(SysUser sysUser, @RequestParam(value = "roleidList") String str) {
		List<Long> list = Arrays.stream(str.split(","))
				.map(s -> Long.parseLong(s.trim()))
				.collect(Collectors.toList());

		System.out.println(sysUser.toString());
		System.out.println(list.get(0));
		return sysUserService.update(sysUser, list) != null;
	}


	/**
	 * 更新数据库一条信息
	 *
	 * @param id 主键
	 * @return 数据列表
	 */
	@RequestMapping(value = "resetPassword", method = RequestMethod.POST)
	public boolean resetPassword(Long id) {
		return sysUserService.resetPassword(id);
	}


}